% extracts the data from the excell files, converts the values of the
% labels from pixeis to coordinates (like worm position), normalizes the
% position in relation to a defined position for the PA stimuli so that all
% worms have the stimulus torwards they are heading in the same place and
% saves the data in a matlab file
% PA STIMULI x=7500 y=12500

%% extra functions 
%convertx
%converty

%%
% DON'T FORGET TO CHANGE NUMBER OF FILES IN THE END AND SAVE
% ANALYZIS DESIGN FOR PA STIMULI (LEFT SIDE OF IMAGE);
%%
clear all

load ('C:\Users\Ana\Dropbox\Matlab\data\labels.mat');

% select the number of worms you are analyzing at the experiment
nworms = 4; %modify this value at the begining of the analyzis

% stores the data from the excell file 
store = cell (nworms,30,1);

for i =1:nworms

% select the excell file from where to extract the data
[filename, pathname] = uigetfile({'*.m';'*.slx';'*.mat';'*.*'},'File Selector');

number = xlsread (filename,1);
time = xlsread (filename ,2);
xpos = xlsread (filename,3);
ypos = xlsread (filename,4);
speed = xlsread (filename,5);
[num, text, raw] = xlsread (filename,6);%raw extract both numerical and string data
reversals = raw (:,1);
[num1, text1, raw1] = xlsread (filename,7);
omegas = raw1 (:,1);
head_bending = xlsread (filename,8);
body_bending = xlsread (filename,9);
label_x = xlsread (filename,10);
label_y = xlsread (filename,11);
time_to_arrive = xlsread (filename,12);
tamanho = xlsread (filename,13);
conversion = xlsread (filename,14);

store {i,1,1} = number;
store {i,2,1} = time;
store {i,3,1} = xpos;
store {i,4,1} = ypos;
store {i,5,1} = speed;
store {i,6,1} = reversals;
store {i,7,1} = omegas;
store {i,8,1} = head_bending;
store {i,9,1} = body_bending;
store {i,10,1} = label_x;
store {i,11,1} = label_y;
store {i,12,1} = time_to_arrive;
store {i,13,1} = tamanho;
store {i,14,1} = conversion;
%%
%transform the value of the labels from pixeis to coordinates and store on
%the cell array

xlabpix = store {i,10,1};
ylabpix = store {i,11,1};
conver = store {i,14,1};

xlab = convertx (xlabpix,conver);
ylab = converty (ylabpix, conver);

store {i,15,1} = xlab;
store {i,16,1} = ylab;

%% find the parameters to align to the PA stimuli
%set the same value for the position of the PA stimuli - I decided these
%values in an arbitrary way to fit well in the graph
PAx = 7500;%new value for PAx
PAy = 12500;%new value for PAy

%find the distance between where the stimulus is in my video and the specificed PA stimuli
xdistPA = PAx - xlab;
ydistPA = PAy - ylab;

%% correct the values of x and y torwards the new stimulus values
hor = store {i,3,1};% to extract data from array
n = size (hor, 1);
horfinal = [];%for x values horizontally
for j = 1:n;
    hornorm = hor (j) + xdistPA;
    a = hornorm;
    horfinal = [horfinal; a];
end

ver = store {i,4,1};% to extract data from array
verfinal = [];%for y values vertically
for k = 1:n;
   vernorm = ver (k) + ydistPA;
    b = vernorm;
    verfinal = [verfinal; b];
end

store {i,17,1} = horfinal;
store {i,18,1} = verfinal;
store {i,23,1} = PAx;
store {i,24,1} = PAy;

end

% save the data in the designated file
RRF3_exp30_ZC2834_OP_to_PA_F1 = [ labels; store ];



